<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

class popular {

	function defaultAction(){		
		$tpl = new Template("movie/popular.htm" );
		$db  = getDB();
		$url = getUrl();	

		//显示近两周最多人“喜欢”的电影，显示排名前20的电影。1）根据近两周上映的电影的“喜欢/5.5以上”的数量进行排序，倒序排序。
		$weektime = strtotime(date('Y-m-d',time()))  - 86400 * 14 ;
		$sql = "SELECT mid,rating,count(mid) AS cnum FROM movie_comments WHERE addtime >= '$weektime' AND rating >= 5 GROUP BY mid ORDER BY cnum DESC LIMIT 0,20";
		$db->query($sql);
		$comments = $db->fetchAll();
		
		foreach($comments as $k => $v){			
			$mids .= "'".$v['mid']."',";
			$mTemp[$v['mid']]['mid'] = $v['mid'];
			$mTemp[$v['mid']]['num'] = $v['cnum'];
			$mTemp[$v['mid']]['rating'] = $v['rating'];
		}
		$mids = substr($mids,0,-1);	

		$param['table'] = 'movie ';
		$param['field'] = ' id,title,subdesc,thumb,story,ratings ';
		$param['where'] = " id IN ($mids) ";
		//$param['order'] = 'ratings DESC';
		$param['page']= '1';		
		$param['rows'] = '20';	
		$popular_movie = $db->getList($param); 

		if(!empty($popular_movie)){
			foreach($popular_movie as $k=>$v){
				$popular_movie[$k]['num'] = $mTemp[$v['id']]['num'];
				$popular_movie[$k]['story'] = csubstr($v['story'],50);
			}
			$popular_movie = multi_array_sort($popular_movie,'num',SORT_DESC);
			foreach($popular_movie as $k=>$v){
				$popular_movie[$k]['k'] = $k + 1;	
			}
		}
        $tpl->assign(assignUser());
		$tpl->assign("popular_movie", $popular_movie);
		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();
	}
}

?>
